package day18.homework;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws Exception {
        Object obj = Class.forName("day18.homework.Person").newInstance();
        Method getName = obj.getClass().getMethod("getName");
        Method getAge = obj.getClass().getMethod("getAge");

        // Declared 获取自身类的，包含私有的。
        // 不加Declared，获取父类和自身的，不包含私有的


        Field nameFiled = obj.getClass().getDeclaredField("name");
        Field ageFiled = obj.getClass().getDeclaredField("age");
        nameFiled.setAccessible(true);
        ageFiled.setAccessible(true);
        nameFiled.set(obj, "小强");
        ageFiled.set(obj, 20);
        System.out.println(getName.invoke(obj));
        System.out.println(getAge.invoke(obj));
        Method setName = obj.getClass().getMethod("setName", String.class);
        Method setAge = obj.getClass().getMethod("setAge", int.class);
        setName.invoke(obj, "大强");
        setAge.invoke(obj, 50);
        System.out.println(getName.invoke(obj));
        System.out.println(getAge.invoke(obj));
    }
}
